package sharemem

import (
	"log"
	"server_cluster/battle/battle_config"
)

// Index结构全部由GC管理
type Config_Index struct {
	Btgp_map   map[int32]*BattleGroup
	Bt_map     map[int32]*Battle
	Btcpt_map  map[int32]*BattleCaptain
	Bf_map     map[int32]*Buff
	Mst_map    map[int32]*Monster
	Mstat_map  map[int32]*MonsterAttr
	Sklcd_map  map[int32]*SkillCard
	Sklchd_map map[int32]*SkillChild
	Sklef_map  map[int32]*SkillEffect
	Sklpas_map map[int32]*SkillPassive
}

// Index 全局索引对象
type Index struct {
	Cfg         Config_Index
	ShmUser_map map[int64]*ShmUser // 玩家map key:uid value:Usr指针
}

// 进程内全局变量
var index_game Index

func GetShmUserByUid(uid int64) *ShmUser {
	v, ok := GetShmUserMap()[uid]
	if !ok {
		return nil
	}
	return v
}

// 活跃用户map
func GetShmUserMap() map[int64]*ShmUser {
	if index_game.ShmUser_map == nil {
		index_game.ShmUser_map = make(map[int64]*ShmUser, battle_config.SHM_USER_MAX)
	}
	return index_game.ShmUser_map
}
func SetShmUserMap(id int64, user *ShmUser) {
	user_map := GetShmUserMap()
	user_map[id] = user
}
func DeleteShmUserMap(id int64) {
	delete(GetShmUserMap(), id)
}

// 用户操作map

// 所有配置map
func GetConfigBattleGroupMap() map[int32]*BattleGroup {
	if index_game.Cfg.Btgp_map == nil {
		index_game.Cfg.Btgp_map = make(map[int32]*BattleGroup, BATTLE_GROUP_MAX)
	}
	return index_game.Cfg.Btgp_map
}
func SetConfigBattleGroupMap(id int32, btgp *BattleGroup) {
	GetConfigBattleGroupMap()[id] = btgp
}

func GetConfigBattleMap() map[int32]*Battle {
	if index_game.Cfg.Bt_map == nil {
		index_game.Cfg.Bt_map = make(map[int32]*Battle, BATTLE_MAX)
	}
	return index_game.Cfg.Bt_map
}
func SetConfigBattleMap(id int32, bt *Battle) {
	GetConfigBattleMap()[id] = bt
}

func GetConfigBattleCaptainMap() map[int32]*BattleCaptain {
	if index_game.Cfg.Btcpt_map == nil {
		index_game.Cfg.Btcpt_map = make(map[int32]*BattleCaptain, BATTLE_CAPTAIN)
	}
	return index_game.Cfg.Btcpt_map
}
func SetConfigBattleCaptainMap(id int32, bt *BattleCaptain) {
	GetConfigBattleCaptainMap()[id] = bt
}

func GetConfigBuffMap() map[int32]*Buff {
	if index_game.Cfg.Bf_map == nil {
		index_game.Cfg.Bf_map = make(map[int32]*Buff, BUFF_MAX)
	}
	return index_game.Cfg.Bf_map
}
func SetConfigBuffMap(id int32, bf *Buff) {
	GetConfigBuffMap()[id] = bf
}
func GetConfigMonsterMap() map[int32]*Monster {
	if index_game.Cfg.Mst_map == nil {
		index_game.Cfg.Mst_map = make(map[int32]*Monster, MONSTER_MAX)
	}
	return index_game.Cfg.Mst_map
}
func SetConfigMonsterMap(id int32, m *Monster) {
	GetConfigMonsterMap()[id] = m
}
func GetConfigMonsterAttrMap() map[int32]*MonsterAttr {
	if index_game.Cfg.Mstat_map == nil {
		index_game.Cfg.Mstat_map = make(map[int32]*MonsterAttr, MONSTER_ATTR_MAX)
	}
	return index_game.Cfg.Mstat_map
}
func SetConfigMonsterAttrMap(id int32, ma *MonsterAttr) {
	GetConfigMonsterAttrMap()[id] = ma
}
func GetConfigSkillEffectMap() map[int32]*SkillEffect {
	if index_game.Cfg.Sklef_map == nil {
		index_game.Cfg.Sklef_map = make(map[int32]*SkillEffect, SKILL_EFFECT_MAX)
	}
	return index_game.Cfg.Sklef_map
}
func SetConfigSkillEffectMap(id int32, se *SkillEffect) {
	GetConfigSkillEffectMap()[id] = se
}

func GetConfigSkillCardMap() map[int32]*SkillCard {
	if index_game.Cfg.Sklcd_map == nil {
		index_game.Cfg.Sklcd_map = make(map[int32]*SkillCard, SKILL_CARD_MAX)
	}
	return index_game.Cfg.Sklcd_map
}
func SetConfigSkillCardMap(id int32, se *SkillCard) {
	GetConfigSkillCardMap()[id] = se
}

func GetConfigSkillChildMap() map[int32]*SkillChild {
	if index_game.Cfg.Sklchd_map == nil {
		index_game.Cfg.Sklchd_map = make(map[int32]*SkillChild, SKILL_CHILD_MAX)
	}
	return index_game.Cfg.Sklchd_map
}
func SetConfigSkillChildMap(id int32, se *SkillChild) {
	GetConfigSkillChildMap()[id] = se
}

func GetConfigSkillPassiveMap() map[int32]*SkillPassive {
	if index_game.Cfg.Sklpas_map == nil {
		index_game.Cfg.Sklpas_map = make(map[int32]*SkillPassive, SKILL_PASSIVE_MAX)
	}
	return index_game.Cfg.Sklpas_map
}
func SetConfigSkillPassiveMap(id int32, se *SkillPassive) {
	GetConfigSkillPassiveMap()[id] = se
}

func (u *ShmUser) DeleteShmUserAllMap() {
	//u.DeleteActionMap()
}

// 初始化user map
func Init_all_user_index() {
	var i int32
	for i = 0; i < battle_config.SHM_USER_MAX; i++ {
		if Myshm.ShmUser[i].IsUse() {
			SetShmUserMap(Myshm.ShmUser[i].GetId(), &(Myshm.ShmUser[i]))
		}
	}
}

// 初始化配置map
func Init_config_index() {
	var i int16
	for i = 0; i < BATTLE_GROUP_MAX; i++ {
		if Myshm.Cfg.Btgp[i].isUse() {
			SetConfigBattleGroupMap(Myshm.Cfg.Btgp[i].getId(), &(Myshm.Cfg.Btgp[i]))
		}
	}
	log.Println("init config battle group map ok.")
	for i = 0; i < BATTLE_MAX; i++ {
		if Myshm.Cfg.Bt[i].isUse() {
			SetConfigBattleMap(Myshm.Cfg.Bt[i].getId(), &(Myshm.Cfg.Bt[i]))
		}
	}
	log.Println("init config battle map ok.")
	for i = 0; i < BATTLE_CAPTAIN; i++ {
		if Myshm.Cfg.Btcpt[i].isUse() {
			SetConfigBattleCaptainMap(Myshm.Cfg.Btcpt[i].getId(), &(Myshm.Cfg.Btcpt[i]))
		}
	}
	log.Println("init config captain map ok.")
	for i = 0; i < BUFF_MAX; i++ {
		if Myshm.Cfg.Bf[i].isUse() {
			SetConfigBuffMap(Myshm.Cfg.Bf[i].getId(), &(Myshm.Cfg.Bf[i]))
		}
	}
	log.Println("init config buff map ok.")
	for i = 0; i < MONSTER_MAX; i++ {
		if Myshm.Cfg.Mst[i].isUse() {
			SetConfigMonsterMap(Myshm.Cfg.Mst[i].getId(), &(Myshm.Cfg.Mst[i]))
		}
	}
	log.Println("init config monster map ok.")
	for i = 0; i < MONSTER_ATTR_MAX; i++ {
		if Myshm.Cfg.Mstat[i].isUse() {
			SetConfigMonsterAttrMap(Myshm.Cfg.Mstat[i].getId(), &(Myshm.Cfg.Mstat[i]))
		}
	}
	log.Println("init config monster_attr map ok.")
	for i = 0; i < SKILL_EFFECT_MAX; i++ {
		if Myshm.Cfg.Sklef[i].isUse() {
			SetConfigSkillEffectMap(Myshm.Cfg.Sklef[i].getId(), &(Myshm.Cfg.Sklef[i]))
		}
	}
	log.Println("init config skill_effect map ok.")
	for i = 0; i < SKILL_PASSIVE_MAX; i++ {
		if Myshm.Cfg.Sklpas[i].isUse() {
			SetConfigSkillPassiveMap(Myshm.Cfg.Sklpas[i].getId(), &(Myshm.Cfg.Sklpas[i]))
		}
	}
	log.Println("init config passive map ok.")
	for i = 0; i < SKILL_CARD_MAX; i++ {
		if Myshm.Cfg.Sklcd[i].isUse() {
			SetConfigSkillCardMap(Myshm.Cfg.Sklcd[i].getId(), &(Myshm.Cfg.Sklcd[i]))
		}
	}
	log.Println("init config skill_card map ok.")
	for i = 0; i < SKILL_CHILD_MAX; i++ {
		if Myshm.Cfg.Sklchd[i].isUse() {
			SetConfigSkillChildMap(Myshm.Cfg.Sklchd[i].getId(), &(Myshm.Cfg.Sklchd[i]))
		}
	}
	log.Println("init config skill_child map ok.")
}
